package com.hctforgreen.greenservice.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.hctforgreen.greenservice.model.LoadingInfoEntity;

public class WelcomePicUtil {
	
	/**
	 * 保存Welcome pic
	 * 
	 * @param activity
	 */
	public static void save(Activity activity, LoadingInfoEntity entity) {
		SharedPreferences sharedPreferences = activity.getSharedPreferences(
				HctConstants.WELCOME_PIC_STORE_FILE_NAME,
				Context.MODE_WORLD_WRITEABLE);
		Editor editor = sharedPreferences.edit();

		String userInfo = change2String(entity);
		editor.putString(HctConstants.WELCOME_PIC_STORE_KEY_VALUE, userInfo);
		editor.commit();
	}
	
	public static LoadingInfoEntity get(Activity activity) {
		SharedPreferences sharedPreferences = activity.getSharedPreferences(
				HctConstants.WELCOME_PIC_STORE_FILE_NAME,
				Context.MODE_WORLD_WRITEABLE);
		String sLoadingInfo = sharedPreferences.getString(
				HctConstants.WELCOME_PIC_STORE_KEY_VALUE, "");
		return change2Entity(sLoadingInfo);
	}
	
	private static String change2String(LoadingInfoEntity entity) {
		String sLoadingInfo = "";
		sLoadingInfo = entity.id + ";" + entity.message;
		return sLoadingInfo;
	}
	
	private static LoadingInfoEntity change2Entity(String sLoadingInfo) {
		String[] strings = sLoadingInfo.split(";");
		LoadingInfoEntity entity = new LoadingInfoEntity();
		if (strings != null && strings.length == 2) {
			entity.id = strings[0];
			entity.message = strings[1];
		}
		return entity;
	}
}
